package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import CONTROLLERS.DichVuControllers;
import CONTROLLERS.HoaDonControllers;
import ModelEntityFW.DichVuEntitys;
import ModelEntityFW.HoaDonEntitys;
import ModelEntityFW.UserEntitys;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class frmThemDichVu extends JFrame {

	private JPanel contentPane;
	private JTextField txtMaDV;
	private JTextField txtTenDV;
	private JTextField txtDongia;
	
	private static Connection m_objConnection;
	private static UserEntitys m_objSecurityContext;
	private static DichVuEntitys m_objDichVuEntity;
	private DichVuControllers m_objDichVuController;

	/**
	 * Launch the application.


	/**
	 * Create the frame.
	 */
	public frmThemDichVu(Connection objConnection, UserEntitys objSecurity, DichVuEntitys objDichVuEntity) {
		setTitle("Thêm dịch vụ");
		//Khởi tạo đối tượng
	    m_objConnection = objConnection;
	    m_objSecurityContext = objSecurity;
		m_objDichVuEntity = objDichVuEntity;
		m_objDichVuController = new DichVuControllers(m_objConnection, m_objSecurityContext);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 303, 246);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnThem = new JButton("Thêm dịch vụ");
		btnThem.setIcon(new ImageIcon(frmThemDichVu.class.getResource("/images/book_edit.png")));
		btnThem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// Thiết lập thông tin cho Entity
				
				m_objDichVuEntity.setTenDichVu(txtTenDV.getText());
	            m_objDichVuEntity.setDonGia(Float.parseFloat(txtDongia.getText()));
                
                // 4. Truy�?n thông tin cho UserController xử lý
                if (m_objDichVuController == null) {
                	m_objDichVuController = new DichVuControllers(m_objConnection, m_objSecurityContext);
                }

                // Thực thi Thêm hoặc Cập Nhật
                if (m_objDichVuEntity.isbFlag()) {
                	m_objDichVuEntity.setMaDichVu(Integer.parseInt(txtMaDV.getText()));
                    boolean bResult = false;
                    try {
                        bResult = m_objDichVuController.ExecOfUpdateElement(m_objDichVuEntity);
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(getParent(), "Err: " + e, "Thông báo",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                    if (bResult) {
                        JOptionPane.showMessageDialog(getParent(), "Cập nhật hóa đơn thành công!", "Thông báo",
                                JOptionPane.INFORMATION_MESSAGE);

                        frmHoaDon fHD = new frmHoaDon(m_objConnection, m_objSecurityContext);
                        fHD.setVisible(true);
                        dispose();
                    }
                } else {
                    boolean bResult = false;
                    try {
                        bResult = m_objDichVuController.ExecOfInsertElement(m_objDichVuEntity);
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(getParent(), "Err: " + e, "Thông báo",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                    if (bResult) {
                        JOptionPane.showMessageDialog(getParent(), "Thêm hóa đơn thành công!", "Thông báo",
                                JOptionPane.INFORMATION_MESSAGE);

                        frmDichVu fDV = new frmDichVu(m_objConnection, m_objSecurityContext);
                        fDV.setVisible(true);
                        // Giải phóng form
                        dispose();
                    }
                }
			}
		});
		btnThem.setBounds(10, 142, 126, 23);
		contentPane.add(btnThem);
		
		JButton btnDong = new JButton("Đóng");
		btnDong.setIcon(new ImageIcon(frmThemDichVu.class.getResource("/images/cancel.gif")));
		btnDong.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnDong.setBounds(169, 142, 89, 23);
		contentPane.add(btnDong);
		
		JLabel lblNewLabel = new JLabel("Mã dịch vụ:");
		lblNewLabel.setBounds(10, 23, 78, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Tên dịch vụ:");
		lblNewLabel_1.setBounds(10, 61, 78, 14);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Đơn giá:");
		lblNewLabel_2.setBounds(10, 101, 78, 14);
		contentPane.add(lblNewLabel_2);
		
		txtMaDV = new JTextField();
		txtMaDV.setEditable(false);
		txtMaDV.setBounds(105, 20, 153, 20);
		contentPane.add(txtMaDV);
		txtMaDV.setColumns(10);
		
		txtTenDV = new JTextField();
		txtTenDV.setColumns(10);
		txtTenDV.setBounds(105, 61, 153, 20);
		contentPane.add(txtTenDV);
		
		txtDongia = new JTextField();
		txtDongia.setColumns(10);
		txtDongia.setBounds(105, 98, 153, 20);
		contentPane.add(txtDongia);
		if(m_objDichVuEntity.isbFlag()){
			BindingControls(m_objDichVuEntity);
		}
	}
	void BindingControls(DichVuEntitys objDichVuEntity){
		txtMaDV.setText(""+objDichVuEntity.getMaDichVu());
		txtTenDV.setText(objDichVuEntity.getTenDichVu());
		txtDongia.setText(""+objDichVuEntity.getDonGia());
	}
}
